package com.strawcom.charmring.view;

import android.app.Activity;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.strawcom.charmring.R;
import com.strawcom.charmring.util.ViewUtil;

/**
 * 
 * @ClassName: TitleView
 * @Description:自定义标题栏
 * @author liyongqiang
 * @date 2015-3-30 上午12:03:08
 * 
 */
public class TitleView extends FrameLayout implements OnClickListener {

	public RelativeLayout layoutLeft;
	public RelativeLayout layoutCenter;
	public RelativeLayout layoutRight;
	
	private Button btnBack;
	private TextView tvTitle;
	private TextView tvRight;
	private Context context;

	private boolean defaultBack;
	private OnBackListener backListener;

	public TitleView(Context context, AttributeSet attrs) {
		super(context, attrs);
		this.context = context;
		LayoutInflater.from(context).inflate(R.layout.view_title, this);
		defaultBack = true;
		initViews();
	}

	private void initViews() {
		btnBack = (Button) findViewById(R.id.btn);
		tvTitle = (TextView) findViewById(R.id.tv_title);
		ViewUtil.changeFontsStyle(tvTitle, getContext());
		tvRight = (TextView) findViewById(R.id.tv_right);

		layoutLeft = (RelativeLayout) findViewById(R.id.layout_left);
		layoutCenter = (RelativeLayout) findViewById(R.id.layout_center);
		layoutRight = (RelativeLayout) findViewById(R.id.layout_right);

		layoutLeft.setOnClickListener(this);
	}

	public void setLeftView(View view) {
		btnBack.setVisibility(View.GONE);
		layoutLeft.setOnClickListener(null);
		layoutLeft.removeAllViewsInLayout();
		ViewUtil.changeFontsStyle(view, getContext());
		layoutLeft.addView(view);
	}

	public void setRightView(View view) {
		tvRight.setVisibility(View.GONE);
		layoutRight.removeAllViewsInLayout();
		ViewUtil.changeFontsStyle(view, getContext());
		layoutRight.addView(view);
	}

	public void setCenterView(View view) {
		tvTitle.setVisibility(View.GONE);
		layoutCenter.removeAllViewsInLayout();
		ViewUtil.changeFontsStyle(view, getContext());
		layoutCenter.addView(view);
	}

	public void setRightText(String text) {
		tvRight.setText(text);
	}

	public void setRightText(int resId) {
		tvRight.setText(resId);
	}

	public void setTitleText(String text) {
		tvTitle.setText(text);
	}

	public void setTitleText(int resId) {
		tvTitle.setText(resId);
	}

	@Override
	public void onClick(View view) {
		switch (view.getId()) {
		case R.id.layout_left:
			if (defaultBack) {
				((Activity) context).onBackPressed();
			} else {
				backListener.onBack();
			}
			break;
		default:
			break;
		}
	}

	public void setBackListener(OnBackListener backListener) {
		defaultBack = false;
		this.backListener = backListener;
	}

	public interface OnBackListener {
		public void onBack();
	};

}
